/* ----------------------------------------------------------------------------------------------------------------
 *  EntityPool
 * ----------------------------------------------------------------------------------------------------------------
 */
package pure.engine.entity 
{
	import flash.display.Sprite;
	
	import pure.engine.system.ns_despair;
	
	use namespace ns_despair;
	
	/**
	 * @internal
	 * 
	 * Entity Pool
	 * 
	 * @author Pure
	 * 
	 */
public class EntityPool 
{

	
	ns_despair static var deadEntities:Vector.<Entity>   =  new Vector.<Entity>();
	ns_despair static var mirrorList:Vector.<Mirror>      =  new Vector.<Mirror>();
	ns_despair static var fragmentList:Vector.<Fragment>  =  new Vector.<Fragment>();
	
	ns_despair static var m_numRevivable:int, m_numMirror:int, m_numFragment:int;

	

	
	// 生成实体
	ns_despair static function makeEntity(name:String,layer:int):Entity 
	{
		var E:Entity;
		
		if (m_numRevivable > 0) 
		{
			E = deadEntities.pop();
			m_numRevivable--;
		}
		else 
		{
			E = new Entity();
		}
		
		E.m_alive  =  true;
		E.m_visible = true;
		E.m_name   =  name;
		E.m_layer  =  layer;
		EntityManager.register(E);
		
		return E;
	}
	
	
	// 回收实体
	ns_despair static function recycleEntity(E:Entity):void
	{
		deadEntities[m_numRevivable++] = E;
	}
	
	
	
	
	
	
	//======================
	// Mirror
	//======================

	
	// 生成残像
	ns_despair static function makeMirror():Mirror
	{
		if (m_numMirror > 0)
		{
			m_numMirror--
			return mirrorList.pop();
		}
		return new Mirror();
	}

	
	// 回收残像
	ns_despair static function recycleMirror(M:Mirror):void
	{
		mirrorList[m_numMirror++] = M;
	}
	
	
	
	
	
	
	//======================
	// Fragment
	//======================
	
	
	// 生成碎片
	ns_despair static function makeFragment():Fragment
	{
		if (m_numFragment > 0)
		{
			m_numFragment--
			return fragmentList.pop();
		}
		return new Fragment();
	}

	
	// 回收碎片
	ns_despair static function recycleFragment(F:Fragment):void
	{
		fragmentList[m_numFragment++] = F;
	}
	
	
}

}